package com.listener;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.support.v4.view.ViewPager.LayoutParams;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.yinhai.activity.ChatActivity;
import com.yinhai.activity.ContactActivity;
import com.yinhai.activity.R;
import com.yinhai.cfg.LoginConfigUtil;
import com.yinhai.constant.Contact;
import com.yinhai.model.ChatMsgEntity;
import com.yinhai.model.MsgModel;
import com.yinhai.service.MsgServiceImpl;

public class MsgTextLongClickListener implements View.OnLongClickListener {
    private MsgServiceImpl msgService;
    
    public final static String SER_KEY = "cn.caiwb.intent.ser";
    
    private PopupWindow resendPopWindow;// 消息弹出框
    
    private List<Map<String, Object>> resendPopList = new ArrayList<Map<String, Object>>();// 消息弹出框数据
    
    private SimpleAdapter resendSimpleAdapter;
    
    private ListView imPopListView;
    
    private View view;
    
    // 列表项弹出窗口
    private PopupWindow mPdw_nav = null;
    
    private String type;
    
    private MsgModel entity;
    
    private Context cxt;
    
    private boolean isGroup;
    
    public MsgTextLongClickListener(String type, MsgModel msgModel, Context cxt) {
        this.type = type;
        this.cxt = cxt;
        this.isGroup = !msgModel.getIsGroup().equals("0");
        this.entity = msgModel;
    }
    
    @Override
    public boolean onLongClick(View arg0) {
        // 初始化itemview弹出框
        LayoutInflater inflater = (LayoutInflater)cxt.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        // TODO Auto-generated method stub
        if ((type != null) && (!"".equals(type))) {
            if (Contact.TEXT == Integer.valueOf(type)) {
                // 当内容是文本时候
                View navWindow = inflater.inflate(R.layout.chat_nav, null);
                View popView = inflater.inflate(R.layout.popwindow_im_add, null);
                initNavWin(navWindow, popView, arg0);
                mPdw_nav.showAsDropDown(arg0);
            } else if (Contact.REDIO == Integer.valueOf(type)) {
                // 当内容是语音时
                View navWindow = inflater.inflate(R.layout.chatvoice_nav, null);
                initNavVoiceWin(navWindow);
                mPdw_nav.showAsDropDown(arg0);
            } else if (Contact.PHOTO == Integer.valueOf(type)) {
                // 当发送的内容是图片时
                View navWindow = inflater.inflate(R.layout.chatpic_nav, null);
                View popView = inflater.inflate(R.layout.popwindow_im_add, null);
                initNavPicWin(navWindow, popView, arg0);
                mPdw_nav.showAsDropDown(arg0);
                
            } else if (Contact.POSMAP == Integer.valueOf(type)) {
                // 当发送的内容是位置时
                View navWindow = inflater.inflate(R.layout.chatpic_nav, null);
                View popView = inflater.inflate(R.layout.popwindow_im_add, null);
                initNavPicWin(navWindow, popView, arg0);
                mPdw_nav.showAsDropDown(arg0);
                
            }
        }
        return true;// 设置为true时，单击事件和长按事件不会同时调用
    }
    
    private void initNavWin(View navWin, View popView, View arg0) {
        view = arg0;
        mPdw_nav = new PopupWindow(navWin, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        TextView navCopy = (TextView)navWin.findViewById(R.id.chat_nav_copy);
        TextView navResend = (TextView)navWin.findViewById(R.id.chat_nav_resend);
        TextView navDel = (TextView)navWin.findViewById(R.id.chat_nav_del);
        resendPopWindow = new PopupWindow(popView, 300, LayoutParams.WRAP_CONTENT);
        imPopListView = (ListView)popView.findViewById(R.id.im_group);
        
        // 复制
        navCopy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                // 获取剪贴板管理服务
                ClipboardManager cm = (ClipboardManager)cxt.getSystemService(Context.CLIPBOARD_SERVICE);
                // 将文本数据复制到剪贴板
                cm.setText(entity.getContent());
                Toast.makeText(cxt, "已复制", Toast.LENGTH_SHORT).show();
            }
        });
        // 转发
        navResend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                nav_resend();
            }
        });
        // 删除
        navDel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                nav_del();
            }
        });
        
        // 点击弹出层外自动消失
        mPdw_nav.setBackgroundDrawable(cxt.getResources().getDrawable(R.drawable.dial_dialog_bgup));
        mPdw_nav.setOutsideTouchable(true);
        resendPopWindow.setFocusable(true);
        resendPopWindow.setBackgroundDrawable(cxt.getResources().getDrawable(R.drawable.dial_dialog_bgup));
        resendPopWindow.setOutsideTouchable(true);
    }
    
    private void initNavPicWin(View navWin, View popView, View arg0) {
        view = arg0;
        mPdw_nav = new PopupWindow(navWin, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        TextView navResend = (TextView)navWin.findViewById(R.id.chat_nav_resend);
        TextView navDel = (TextView)navWin.findViewById(R.id.chat_nav_del);
        resendPopWindow = new PopupWindow(popView, 300, LayoutParams.WRAP_CONTENT);
        imPopListView = (ListView)popView.findViewById(R.id.im_group);
        // 转发
        navResend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                nav_resend();
            }
        });
        // 删除
        navDel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                nav_del();
            }
        });
        // 点击弹出层外自动消失
        mPdw_nav.setBackgroundDrawable(cxt.getResources().getDrawable(R.drawable.dial_dialog_bgup));
        mPdw_nav.setOutsideTouchable(true);
        resendPopWindow.setFocusable(true);
        resendPopWindow.setBackgroundDrawable(cxt.getResources().getDrawable(R.drawable.dial_dialog_bgup));
        resendPopWindow.setOutsideTouchable(true);
    }
    
    private void initNavVoiceWin(View navWin) {
        mPdw_nav = new PopupWindow(navWin, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        TextView navDel = (TextView)navWin.findViewById(R.id.chat_nav_del);
        
        // 删除
        navDel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                nav_del();
            }
        });
        // 点击弹出层外自动消失
        mPdw_nav.setBackgroundDrawable(cxt.getResources().getDrawable(R.drawable.dial_dialog_bgup));
        mPdw_nav.setOutsideTouchable(true);
    }
    
    private void nav_del() {
        
        msgService = new MsgServiceImpl(cxt);
        String sendUserID = entity.getSendUserId();
        String receUserID = entity.getRecId();
        if (entity.getRecId().equals(LoginConfigUtil.getConfig(cxt).getUsername())) {
            sendUserID = entity.getRecId();
            receUserID = entity.getSendUserId();
        }
        
        List<ChatMsgEntity> list = msgService.findSimgleMessage(sendUserID, receUserID, isGroup);
        if (list.size() == 1) {
            MsgModel mm = msgService.getMsgModelByUUID(entity.getUuid());
            if (isGroup) {
                msgService.delIMListByGroup(sendUserID, receUserID);
            } else {
                msgService.delIMListByP2P(mm);
            }
        } else {
            if (list.size() > 0 && list.get(list.size() - 1).getMsgModel().getUuid().equals(entity.getUuid())) {
                ChatMsgEntity cme = list.get(list.size() - 1);
                MsgModel mgm = msgService.getMsgModelByUUID(cme.getUuid());
                String cont = cme.getText();
                if (cme.getType().equals("2")) {
                    cont = "[图片]";
                } else if (cme.getType().equals("3")) {
                    cont = "[语音]";
                } else if (cme.getType().equals("4")) {
                    cont = "[位置]";
                }
                if (isGroup) {
                    msgService.updateShowContentGroup(mgm, cont);
                } else {
                    msgService.updateShowContentP2P(mgm, cont);
                }
            }
        }
        msgService.deleteMsgModelByUUID(entity.getUuid());
        mPdw_nav.dismiss();
        
        Intent mIntent1 = new Intent("com.im.delmessage");
        mIntent1.putExtra(SER_KEY, entity);
        cxt.sendBroadcast(mIntent1);
    }
    
    private void nav_resend() {
        msgService = new MsgServiceImpl(cxt);
        MsgModel msgModel = msgService.getMsgModelByUUID(entity.getUuid());
        Intent intent = new Intent(cxt, ContactActivity.class);
        intent.putExtra("userType", 3);
        intent.putExtra("isMutiCheck", false);
        intent.putExtra(SER_KEY, msgModel);
        cxt.startActivity(intent);
        ((ChatActivity)cxt).finish();
        // if (!resendPopList.isEmpty()) {
        // resendPopList.clear();
        // }
        // if (resendPopList.isEmpty()) {
        //
        // Map<String, Object> item = new HashMap<String, Object>();
        // item.put("title", "发起群聊");
        // item.put("type", "0");
        // resendPopList.add(item);
        //
        // // 获取所有的群组
        // ContactUserServiceImpl contactUserService = new ContactUserServiceImpl(cxt);
        // List<ContactGroup> contactGroups = contactUserService.findGroup(null);
        // for (ContactGroup contactGroup : contactGroups) {
        // Map<String, Object> map = new HashMap<String, Object>();
        // map.put("title", contactGroup.getName());
        // map.put("type", 1);
        // map.put("data", contactGroup);
        // resendPopList.add(map);
        // }
        //
        // resendSimpleAdapter =
        // new SimpleAdapter(cxt, resendPopList, R.layout.popupwindow_im_list_item, new String[] {"title"},
        // new int[] {R.id.title});
        //
        // imPopListView.setAdapter(resendSimpleAdapter);
        // imPopListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        //
        // @Override
        // public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
        // if (resendPopWindow != null) {
        // resendPopWindow.dismiss();
        // }
        // Map<String, Object> item = resendPopList.get(arg2);
        // String itemtype = item.get("type").toString();
        // msgService = new MsgServiceImpl(cxt);
        // MsgModel msgModel = msgService.getMsgModelByUUID(entity.getUuid());
        // if (Contact.TEXT == Integer.valueOf(type) || Contact.POSMAP == Integer.valueOf(type)) {
        // if (itemtype.equals("1")) {
        // Intent intent = new Intent(cxt, ContactGroupAcitivty.class);
        // intent.putExtra("groupID", ((ContactGroup)item.get("data")).getGroupId());
        // Bundle mBundle = new Bundle();
        // mBundle.putSerializable(SER_KEY, msgModel);
        // intent.putExtras(mBundle);
        // cxt.startActivity(intent);
        // } else {
        // Intent intent = new Intent(cxt, ChatGroupActivity.class);
        // Bundle mBundle = new Bundle();
        // mBundle.putSerializable(SER_KEY, msgModel);
        // intent.putExtras(mBundle);
        // cxt.startActivity(intent);
        // }
        // } else if (Contact.PHOTO == Integer.valueOf(type)) {
        // // File f = new File(Photo.getSaveIMPicPath(), msgModel.getUuid() + ".png");
        // // if (f.exists()) {
        // if (itemtype.equals("1")) {
        // Intent intent = new Intent(cxt, ContactGroupAcitivty.class);
        // intent.putExtra("groupID", ((ContactGroup)item.get("data")).getGroupId());
        // Bundle mBundle = new Bundle();
        // mBundle.putSerializable(SER_KEY, msgModel);
        // intent.putExtras(mBundle);
        // cxt.startActivity(intent);
        // } else {
        // Intent intent = new Intent(cxt, ChatGroupActivity.class);
        // Bundle mBundle = new Bundle();
        // mBundle.putSerializable(SER_KEY, msgModel);
        // intent.putExtras(mBundle);
        // cxt.startActivity(intent);
        // }
        // // } else {
        // // Toast.makeText(cxt, "您没有获取对方图片，无法转发", Toast.LENGTH_SHORT).show();
        // // }
        // }
        //
        // }
        // });
        //
        // }
        // if (mPdw_nav != null && mPdw_nav.isShowing()) {
        // mPdw_nav.dismiss();
        // }
        // resendPopWindow.showAsDropDown(view, 0, 0);
    }
    
}
